package org.nf.chat.websocket;





import org.nf.chat.model.User;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

/**
 * 握手连接的设置类
 */
public class WebSocketHandshake extends Configurator {

    /**
     * 修改握手方法，在这里面获取HttpSession的信息
     * */
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        //获取HttpSession
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        //获取登陆的用户信息
        User user = (User) httpSession.getAttribute("user");
        //将用户信息中转保存在WebSocket连接用户的Session属性中
        sec.getUserProperties().put("user", user);
    }
}
